package com.haiheng.voiceandbook.view.shape;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.haiheng.voiceandbook.R;


/**
 * Created by chow on 2019/8/10
 */

public class ShapeLinearLayout extends LinearLayout {

    private ShapeHelper shapeHelper;

    private int selectedTextColor;

    private int normalTextColor;

    private int strokeColor;

    private int solidColor;

    private int strokeWidth;

    private ShapeBuilder shapeBuilder;


    private float cornersRadius;

    private float cornersTopLeftRadius;

    private float cornersTopRightRadius;

    private float cornersBottomLeftRadius;

    private float cornersBottomRightRadius;


    public ShapeLinearLayout(Context context) {
        this(context, null);
    }

    public ShapeLinearLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShapeLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        shapeBuilder = new ShapeBuilder();
        init(context, attrs, defStyleAttr);

    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ShapeLinearLayout, defStyleAttr, 0);
        // 公共
        cornersRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shapeCornersRadius, 0); // 圆角
        cornersTopLeftRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shadowCornersTopLeftRadius, 0); // 左上圆角
        cornersTopRightRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shadowCornersTopRightRadius, 0); // 右上圆角
        cornersBottomRightRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shadowCornersBottomRightRadius, 0); // 右下圆角
        cornersBottomLeftRadius = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shadowCornersBottomLeftRadius, 0); // 左下圆角
        solidColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeSolidColor, 0); // 填充色
        strokeWidth = typedArray.getDimensionPixelOffset(R.styleable.ShapeLinearLayout_shapeStrokeWidth, 0); // 描边宽度
        strokeColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeStrokeColor, 0); // 描边色
        // 按压
        int pressSolidColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeSelectorPressedColor, 0);// 填充色
        // 选中
        int selectedSolidColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeSelectorNormalColor, 0);// 填充色
        int selectedStrokeColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeSelectorNormalColor, 0);// 描边色

        //字体颜色
//        normalTextColor = getCurrentTextColor();
        selectedTextColor = typedArray.getColor(R.styleable.ShapeLinearLayout_shapeSelectorNormalColor, 0);
        selectedTextColor = selectedTextColor == 0 ? normalTextColor : selectedTextColor;

        typedArray.recycle();

        shapeHelper = new ShapeHelper(this)
                .setRadius(new float[]{cornersTopLeftRadius, cornersTopLeftRadius,
                        cornersTopRightRadius, cornersTopRightRadius, cornersBottomRightRadius, cornersBottomRightRadius, cornersBottomLeftRadius, cornersBottomLeftRadius})
                .setRadius(cornersRadius).setStrokeWidth(strokeWidth)
                .setNormalDrawable(strokeColor, solidColor)
                .setPressDrawable(0, pressSolidColor)
                .setSelectedDrawable(selectedStrokeColor, selectedSolidColor);
        shapeHelper.setNormalDrawable();
        initShape();
    }

    private void initShape() {
        shapeBuilder
                .setShapeType(ShapeBuilder.RECTANGLE)
                .setShapeCornersRadius(cornersRadius)
                .setShapeCornersTopLeftRadius(cornersTopLeftRadius)
                .setShapeCornersTopRightRadius(cornersTopRightRadius)
                .setShapeCornersBottomRightRadius(cornersBottomRightRadius)
                .setShapeCornersBottomLeftRadius(cornersBottomLeftRadius)
                .setShapeSolidColor(solidColor)
                .setShapeStrokeColor(strokeColor)
                .setShapeStrokeWidth(strokeWidth)
//                .setShapeStrokeDashWidth(strokeDashWidth)
//                .setShapeStrokeDashGap(strokeDashGap)
                .setShapeUseSelector(true)
//                .setShapeSelectorNormalColor(selectorNormalColor)
//                .setShapeSelectorPressedColor(selectorPressedColor)
                .into(this);
    }



    @Override
    public void setPressed(boolean pressed) {
        super.setPressed(pressed);
        if (pressed) {
            shapeHelper.setPressDrawable();
        }
    }



    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            shapeHelper.setSelectedDrawable();
//            setTextColor(selectedTextColor);
        } else {
            shapeHelper.setNormalDrawable();
//            setTextColor(normalTextColor);
        }
    }

    public void setSolidColor(int color) {
        this.solidColor = color;
        shapeHelper.setNormalDrawable(strokeColor, color);
    }

    public void setStrokeColor(int strokeColor) {
        this.strokeColor = strokeColor;
        shapeHelper.setStrokeColor(strokeColor);
        shapeHelper.setNormalDrawable(strokeColor, solidColor);
    }

    public void setStrokeWidth(int strokeWidth) {
        shapeHelper.setStrokeWidth(strokeWidth);
    }

    public void setRadius(int radius) {
        shapeHelper.setRadius(radius);
    }

}